#include "handwritingframe.h"
#include "handwritingframe_p.h"
#include "ui_handwritingframe.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QInputDialog>
#include <QColorDialog>

HandWritingFramePrivate::HandWritingFramePrivate(HandWritingFrame *parent) :
    QObject(parent),
    q(parent)
{
    area = new ScribbleArea(parent);
}

HandWritingFramePrivate::~HandWritingFramePrivate()
{
    delete area;
}

void HandWritingFramePrivate::initializeMember()
{
    QVBoxLayout *leftVerticalLayout = new QVBoxLayout();

    leftVerticalLayout->addWidget(q->ui->chooseLineColor);
    leftVerticalLayout->addWidget(q->ui->chooseLineBold);
    leftVerticalLayout->addWidget(q->ui->clear);

    QVBoxLayout *rightVerticalLayout = new QVBoxLayout();

    rightVerticalLayout->addWidget(q->ui->exportToFile);
    rightVerticalLayout->addWidget(q->ui->changeToType);
    rightVerticalLayout->addWidget(q->ui->send);

    QHBoxLayout *horizontalLayout = new QHBoxLayout(q);

    horizontalLayout->addLayout(leftVerticalLayout);
    horizontalLayout->addWidget(area);
    horizontalLayout->addLayout(rightVerticalLayout);
}

void HandWritingFramePrivate::connectSignalsAndSlots()
{
    connect(this,                   SIGNAL(sendMessage(QVariant)),
            q,                      SIGNAL(sendMessage(QVariant)));
    connect(q->ui->send,            SIGNAL(clicked()),
            this,                   SLOT(returnPressed()));
    connect(q->ui->chooseLineBold,  SIGNAL(clicked()),
            this,                   SLOT(setPenWidth()));
    connect(q->ui->chooseLineColor, SIGNAL(clicked()),
            this,                   SLOT(setPenColor()));
    connect(q->ui->clear,           SIGNAL(clicked()),
            area,                   SLOT(clear()));
}

void HandWritingFramePrivate::completeConstruct()
{

}

void HandWritingFramePrivate::returnPressed()
{
    if(area->isModified())
    {
        const QImage handWritingMesg = area->image();

        if(!handWritingMesg.isNull())
            emit sendMessage(handWritingMesg);
    }

    area->clear();
}

void HandWritingFramePrivate::setPenWidth()
{
    bool ok;
    int newWidth = QInputDialog::getInteger(q, tr("设置笔迹宽度"),
                                            tr("请填写笔迹宽度（像素）："),
                                            area->penWidth(),
                                            1, 50, 1, &ok);
    if(ok) area->setPenWidth(newWidth);
}

void HandWritingFramePrivate::setPenColor()
{
    QColor newColor = QColorDialog::getColor(area->penColor(), q, tr("设置笔迹颜色"));

    if (newColor.isValid())
        area->setPenColor(newColor);
}

HandWritingFrame::HandWritingFrame(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::HandWritingFrame),
    d(new HandWritingFramePrivate(this))
{
    ui->setupUi(this);

    d->initializeMember();
    d->connectSignalsAndSlots();
    d->completeConstruct();
}

HandWritingFrame::~HandWritingFrame()
{
    delete ui;
    delete d;
}

QPushButton *HandWritingFrame::changeToType() const
{
    return ui->changeToType;
}

void HandWritingFrame::changeEvent(QEvent *e)
{
    QFrame::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
